package com.sskj.one;

import android.app.AlertDialog;
import android.content.Context;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.sskj.common.base.App;
import com.sskj.common.base.AppManager;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.lib.util.TipUtil;
import com.sskj.one.component.DaggerUserDataComponent;
import com.sskj.one.login.LoginActivity;
import com.sskj.one.view.TipDialog;

import javax.inject.Inject;

import cn.jpush.android.api.JPushInterface;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

@Route(path = "/app/service/logout")
public class LogoutProviderImpl implements LogoutProvider {
    @Inject
    UserViewModel userViewModel;

    @Override
    public void logout() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.clear();
        JPushInterface.deleteAlias(App.INSTANCE, 0);
        SPUtil.clear();
        AppManager.getAppManager().currentActivity();
        HttpHeaders commonHeaders = OkGo.getInstance().getCommonHeaders();
//        HttpParams commonParams = OkGo.getInstance().getCommonParams();
//        commonParams.clear();
        commonHeaders.clear();
        AppManager.getAppManager().finishAllActivity();
        LoginActivity.start(App.INSTANCE);
    }

    @Override
    public void logout(String msg) {
        DaggerUserDataComponent.create().inject(this);
        Observable.just(0)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        if (AppManager.getAppManager().currentActivity() != null) {
                            TipUtil.getExitTip(AppManager.getAppManager().currentActivity(), new TipUtil.OnTipSure() {
                                @Override
                                public void onSure() {
                                    userViewModel.clear();
                                    JPushInterface.deleteAlias(App.INSTANCE, 0);
                                    SPUtil.clear();
                                    AppManager.getAppManager().currentActivity();
                                    HttpHeaders commonHeaders = OkGo.getInstance().getCommonHeaders();
                                    commonHeaders.clear();
                                    AppManager.getAppManager().finishAllActivity();
                                    LoginActivity.start(App.INSTANCE);
                                }
                            });
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void init(Context context) {

    }
}
